/**
 * Created by whw on 2017/1/11.
 */
$(function () {
    var dataTypes = '',
        getUrl = '/shop/getAllShop',
        businessType = getQueryString('businessType') || '',
        provideNumReversal = getQueryString('provideNumReversal') || '',
        zhaoNiuReportReversal = getQueryString('zhaoNiuReportReversal') || '',
        shopName = getQueryString('shopName') || '',
        jrollinit = null,
        zanwu = '',
        setmeatType = function (obj) {
            obj.businessType = obj.businessType ? obj.businessType : '';
            obj.pageSize = obj.pageSize ? obj.pageSize : 10;
            obj.offset = obj.offset ? obj.offset : 0;
            obj.provideNumReversal = provideNumReversal;
            obj.shopName = obj.shopName || '';
            obj.zhaoNiuReportReversal =zhaoNiuReportReversal;
            dataTypes = 'businessType='+obj.businessType+'&pageSize='+obj.pageSize+'&offset='+obj.offset+ '&provideNumReversal='+obj.provideNumReversal+'&shopName='+obj.shopName+'&zhaoNiuReportReversal='+obj.zhaoNiuReportReversal
            return dataTypes;
        },
        requestData = function (obj,fun) {
            $.ajax({
                url: host + getUrl,
                type: 'post',
                data: obj,
                success: function (data) {
                    fun(data)
                }
            });
        };
    // 搜索页面
    $('#search input').on('focus', function () {
        searchPublish()
    })
// 轮播图

    var mySwiper = new Swiper('.content1_1_left .swiper-container', {
        // direction: 'vertical',
        loop: true,
        autoplay: 1000,
        speed: 3000,
        loopAdditionalSlides: 1,
        autoplayDisableOnInteraction: false,
        pagination: '.swiper-pagination',
    })
    $('#nav ul li').on('click', function () {     //切换 tab 栏
        if($(this).hasClass('font_color')) {
            return;
        }
        // breakS = true
        // 给导航添加样式
        $('#nav ul li').removeClass('font_color')
        $(this).addClass('font_color')
        if (this.innerText == '羊肉') {
            businessType = 2;
        } else if(this.innerText == '牛肉') {
            businessType = 1
        } else {
            businessType = '';
        }
        jrollinit.destroy();
        $('#meal-data').empty()
        $('#meal-data').append($('<ul class="container" id="scroller"></ul>'));
        jroll();
        jrollinit.refresh();
    });
    $('#nav .term li').eq(0).on('click', function () {
        $('#nav .term li').removeClass('blue-color')
        $(this).addClass('blue-color')
        $('.san').removeClass('toggle-b')
        $('.san').removeClass('toggle-t')
        zhaoNiuReportReversal = '';
        provideNumReversal = '';
        jrollinit.destroy();
        $('#meal-data').empty()
        $('#meal-data').append($('<ul class="container" id="scroller"></ul>'));
        jroll();
        jrollinit.refresh();
    })
    $('#nav .term li').eq(1).on('click', function () {
        $('#nav .term li').removeClass('blue-color')
        $(this).addClass('blue-color')
        zhaoNiuReportReversal = !zhaoNiuReportReversal;
        console.log(zhaoNiuReportReversal)
        if(!zhaoNiuReportReversal) {
            $('.san').eq(1).addClass('toggle-b')
            $('.san').eq(0).removeClass('toggle-t')
        }else {
           $('.san').eq(0).addClass('toggle-t')
            $('.san').eq(1).removeClass('toggle-b')
        }
        // priceReverse = true
        jrollinit.destroy();
        $('#meal-data').empty()
        $('#meal-data').append($('<ul class="container" id="scroller"></ul>'));
        jroll();
        jrollinit.refresh();

    })
    $('#nav .term li').eq(2).on('click', function () {
        $('#nav .term li').removeClass('blue-color')
        $(this).addClass('blue-color')
        provideNumReversal = !provideNumReversal;
        if(!provideNumReversal) {
            $('.san').eq(3).addClass('toggle-b')
            $('.san').eq(2).removeClass('toggle-t')
        }else {
            $('.san').eq(2).addClass('toggle-t')
            $('.san').eq(3).removeClass('toggle-b')
        }
        // priceReverse = true
        jrollinit.destroy();
        $('#meal-data').empty()
        $('#meal-data').append($('<ul class="container" id="scroller"></ul>'));
        jroll();
        jrollinit.refresh();

    })
    jroll();
    function jroll() {
        jrollinit = new JRoll($('#meal-data')[0], {
            scroller:$('#scroller')[0],
            scrollBarY: true,
            scrollBarFade: true
        });   // jroll 对象

        // // 下拉刷新
        jrollinit.pulldown({
            spinning:false,
            refresh: function(complete) {
                jrollinit.options.page = 1;
                requestData(setmeatType({
                    businessType: businessType,
                    offset: jrollinit.options.page - 1,
                    shopName:shopName,
                    provideNumReversal:provideNumReversal,
                    zhaoNiuReportReversal:zhaoNiuReportReversal
                }),function (data) {
                    if (data.success == true) {
                        complete();
                        if(!data.data.length){
                            jrollinit.scroller.innerHTML = '<div id="noIssue" style="text-align: center;margin-top: 100px;"><div><img src="img/noSearch.png"></div><p>搜索无结果</p><div class="jroll-infinite-tip"><a href="javascript:;" id="noresult">换个关键词试试</a></div></div>'
                            $('#noresult').on('click', function () {
                                $('#search input').focus();
                            })
                        }else {
                            jrollinit.scroller.innerHTML = "";    //清空内容
                            jrollinit.infinite_callback(data.data);
                        }
                    } else {
                        //无数据则返回什么
                        console.log(data)
                    }
                });
            },
        });
        // 无限加载
        jrollinit.infinite({
            options: jrollinit,
            completeTip: '<div class=\"jroll-infinite-tip\" style=\"text-align\: center\;\">俺也是有底线的！</div>',
            loadingTip: '<div class=\"jroll-infinite-tip\" style=\"text-align\: center\;\">正在加载。。。</div>',
            template: '<li><div class="title"><div class="shopImg fl"><img src="{{=_obj.shopLogo?_obj.shopLogo:"img/shop/defaultShopImg.png"}}" alt=""></div><div class="mid fl"><h4>{{=_obj.shopName}}</h4><p><span class="totel">商品总数 {{=_obj.provideNum}}</span>   <span class="hot">找牛热度 {{=_obj.zhaoNiuReport || "无"}}</span></p></div><a id="goShop" class="fr" href="personalStore.html?id={{=_obj.userId}}">进店</a></div><div class="imgList {{=_obj.provideInfos[0]?"":"hidden"}}"><div class="{{=_obj.provideInfos[0]?"":"visi"}}"><img data-shopName="{{=_obj.shopName}}"  data-name="{{=_obj.provideInfos[0]?_obj.provideInfos[0].meatCategory[0]?_obj.provideInfos[0].meatCategory[0]:\"暂无":""}}" data-price="{{=_obj.provideInfos[0]?_obj.provideInfos[0].price?_obj.provideInfos[0].price:\"议定":""}}" data-num="{{=_obj.provideInfos[0]?_obj.provideInfos[0].amount?_obj.provideInfos[0].amount:"":""}}" data-changh="{{=_obj.provideInfos[0]?_obj.provideInfos[0].factoryNo?_obj.provideInfos[0].factoryNo:"":""}}" data-suoz="{{=_obj.provideInfos[0]?_obj.provideInfos[0].warehouse?_obj.provideInfos[0].warehouse:"":""}}" data-chandi="{{=_obj.provideInfos[0]?_obj.provideInfos[0].country?_obj.provideInfos[0].country:"":""}}" data-id="{{=_obj.provideInfos[0]?_obj.provideInfos[0].id?_obj.provideInfos[0].id:"":""}}" data-uid="{{=_obj.userId}}" {{=_obj.provideInfos[0]?"jroll-viewer-image":""}} src="{{=_obj.provideInfos[0]?_obj.provideInfos[0].imageList[0]?_obj.provideInfos[0].imageList[0]:"img/defaultImg.png":""}}" alt=""><div class="msg"><span class=fl>{{=_obj.provideInfos[0]?_obj.provideInfos[0].meatCategory[0]?_obj.provideInfos[0].meatCategory[0]:\"暂无":""}}</span><span class=fr style="color:orange;">￥{{=_obj.provideInfos[0]?_obj.provideInfos[0].price?_obj.provideInfos[0].price:\"议定":""}}/吨</span></div></div><div class="{{=_obj.provideInfos[1]?"":"visi"}}"><img data-shopName="{{=_obj.shopName}}" data-name="{{=_obj.provideInfos[1]?_obj.provideInfos[1].meatCategory[0]?_obj.provideInfos[1].meatCategory[0]:\"暂无":""}}" data-price="{{=_obj.provideInfos[1]?_obj.provideInfos[1].price?_obj.provideInfos[1].price:\"议定":""}}" data-num="{{=_obj.provideInfos[1]?_obj.provideInfos[1].amount?_obj.provideInfos[1].amount:"":""}}" data-changh="{{=_obj.provideInfos[1]?_obj.provideInfos[1].factoryNo?_obj.provideInfos[1].factoryNo:"":""}}" data-suoz="{{=_obj.provideInfos[1]?_obj.provideInfos[1].warehouse?_obj.provideInfos[1].warehouse:"":""}}" data-chandi="{{=_obj.provideInfos[1]?_obj.provideInfos[1].country?_obj.provideInfos[1].country:"":""}}" data-id="{{=_obj.provideInfos[1]?_obj.provideInfos[1].id?_obj.provideInfos[1].id:"":""}}" data-uid="{{=_obj.userId}}" {{=_obj.provideInfos[1]?"jroll-viewer-image":""}} src="{{=_obj.provideInfos[1]?_obj.provideInfos[1].imageList[0]?_obj.provideInfos[1].imageList[0]:"img/defaultImg.png":""}}"><div class="msg"><span class=fl>{{=_obj.provideInfos[1]?_obj.provideInfos[1].meatCategory[0]?_obj.provideInfos[1].meatCategory[0]:\"暂无":""}}</span><span class=fr style="color:orange;">￥{{=_obj.provideInfos[1]?_obj.provideInfos[1].price?_obj.provideInfos[1].price:\"议定":""}}/吨</span></div></div><div class="{{=_obj.provideInfos[2]?"":"visi"}}"><img data-shopName="{{=_obj.shopName}}" data-name="{{=_obj.provideInfos[2]?_obj.provideInfos[2].meatCategory[0]?_obj.provideInfos[2].meatCategory[0]:\"暂无":""}}" data-price="{{=_obj.provideInfos[2]?_obj.provideInfos[2].price?_obj.provideInfos[2].price:\"议定":""}}" data-num="{{=_obj.provideInfos[2]?_obj.provideInfos[2].amount?_obj.provideInfos[2].amount:"":""}}" data-changh="{{=_obj.provideInfos[2]?_obj.provideInfos[2].factoryNo?_obj.provideInfos[2].factoryNo:"":""}}" data-suoz="{{=_obj.provideInfos[2]?_obj.provideInfos[2].warehouse?_obj.provideInfos[2].warehouse:"":""}}" data-chandi="{{=_obj.provideInfos[2]?_obj.provideInfos[2].country?_obj.provideInfos[2].country:"":""}}" data-id="{{=_obj.provideInfos[2]?_obj.provideInfos[2].id?_obj.provideInfos[2].id:"":""}}" data-uid="{{=_obj.userId}}" {{=_obj.provideInfos[2]?"jroll-viewer-image":""}} src="{{=_obj.provideInfos[2]?_obj.provideInfos[2].imageList[0]?_obj.provideInfos[2].imageList[0]:"img/defaultImg.png":""}}"><div class="msg"><span class=fl>{{=_obj.provideInfos[2]?_obj.provideInfos[2].meatCategory[0]?_obj.provideInfos[2].meatCategory[0]:\"暂无":""}}</span><span class=fr style="color:orange;">￥{{=_obj.provideInfos[2]?_obj.provideInfos[2].price?_obj.provideInfos[2].price:\"议定":""}}/吨</span></div></div></div></li>',
            getData: function (page, callback) {
                    requestData(setmeatType({
                        businessType: businessType,
                        offset:page-1,
                        shopName:shopName,
                        provideNumReversal:provideNumReversal,
                        zhaoNiuReportReversal:zhaoNiuReportReversal
                    }),function (data) {
                        if (data.success == true) {
                            if(!data.total){
                                jrollinit.options.total = 0;
                                jrollinit.scroller.innerHTML = '<div id="noIssue" style="text-align: center;margin-top: 100px;"><div><img src="img/noSearch.png"></div><p>搜索无结果</p><div class="jroll-infinite-tip"><a href="javascript:;" id="noresult">换个关键词试试</a></div></div>'
                                $('#noresult').on('click', function () {
                                    $('#search input').focus();
                                })
                            }else{
                                jrollinit.options.total = Math.ceil(data.total / 10);
                                if(page >= jrollinit.options.total){
                                    jrollinit.options.page = jrollinit.options.total;
                                }
                                callback(data.data)
                            }

                        } else {
                            //无数据则返回什么
                            console.log(data)
                        }

                    });
            },
        });
        jrollinit.on("refresh", function() {
            var imgListS = $('.imgList');
            imgListS.each(function (v,e) {
                new JRollViewer(e)
            })
        });
        return jrollinit;
    };
    // '<li><div class="title"><div class="shopImg fl"><img src="'+(_obj.shopLogo)+'" alt=""></div><div class="mid fl"><h4>'+(_obj.shopName)+'</h4><p><span class="totel">商品总数 '+(_obj.provideNum)+'</span> <span class="hot">找妞热度 '+(_obj.zhaoNiuReport)+'</span></p></div><a id="goShop" class="fr" href="javascript">进店</a></div><div class="imgList"><div><img src="'+(_obj.provideInfos[0]?_obj.provideInfos[0].imageList[0]?_obj.provideInfos[0].imageList[0]:"img/defaultImg.png":'')+'" alt=""><div class="msg"><span class=fl>'+(_obj.provideInfos[0]?_obj.provideInfos[0].meatCategory[0]?_obj.provideInfos[0].meatCategory[0]:"暂无":'')+'</span><span class=fr style="color:orange;">￥245000.00/吨</span></div></div><div><img src="'+(_obj.provideInfos[0]?_obj.provideInfos[0].imageList[0]?_obj.provideInfos[0].imageList[0]:"img/defaultImg.png":)+'"><div class="msg"><span class=fl>牛肉</span><span class=fr style="color:orange;">￥245000.00/吨</span></div></div><div><img src="'+(_obj.provideInfos[0]?_obj.provideInfos[0].imageList[0]?_obj.provideInfos[0].imageList[0]:"img/defaultImg.png")+'"><div class="msg"><span class=fl>牛肉</span><span class=fr style="color:orange;">￥245000.00/吨</span></div></div></div></li>';
})